package cn.lls.controller.sa;


import cn.lls.base.result.R;
import cn.lls.base.result.ResultCode;
import cn.lls.entity.sys.PermissionEntity;
import cn.lls.service.IPermissionService;
import cn.lls.vo.permission.PermissionVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * <p>
 *  权限控制器
 * </p>
 *
 * @author 划水艺术家
 * @since 2022-11-15
 */

@RestController
@RequestMapping("/sa/permission")
@Slf4j
@Api(tags = "超级管理员-权限控制器")
@CrossOrigin(origins = "*")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    @ApiOperation(value = "添加权限", notes = "增加本项目的权限，页面、菜单等", response = R.class, httpMethod = "POST")
    @PostMapping
    public R addPermission(
            @ApiParam(required = true, value = "权限实体信息")
            @Validated @RequestBody PermissionEntity permission,
            BindingResult bindingResult){
        permissionService.addPermission(permission);
        return R.success();
    }

    @ApiOperation(value = "删除权限", notes = "根据Id删除本项目的权限，当该权限有子权限无法删除", response = R.class, httpMethod = "DELETE")
    @DeleteMapping("/{permissionId}")
    public R delPermission(
            @ApiParam(required = true, value = "权限Id")
            @PathVariable("permissionId")
            @NotEmpty(message = "必须具备权限Id") Integer permissionId){
        permissionService.delPermission(permissionId);
        return R.success();
    }

    @ApiOperation(value = "更改权限", notes = "更改本项目的权限，页面、菜单等", response = R.class, httpMethod = "PUT")
    @PutMapping
    public R updatePermission(
            @ApiParam(required = true, value = "权限实体信息")
            @RequestBody PermissionEntity permission,
            BindingResult bindingResult){
        log.info("更改： permi = {}", permission);
        permissionService.updatePermission(permission);
        return R.success();
    }

    @ApiOperation(value = "根据Id获取权限", notes = "根据Id获取本项目的权限，页面、菜单等",response = R.class, httpMethod = "GET")
    @GetMapping("/{permissionId}")
    public R getPermissionById(
            @ApiParam(required = true, value = "权限Id")
            @PathVariable("permissionId")
            @NotEmpty(message = "必须具备权限Id") Integer permissionId){
        PermissionVO permissionVO = permissionService.getPermissionById(permissionId);
        if (permissionVO == null)
            return R.fail(ResultCode.DATA_ERROR, "传参错误，不包含该Id的数据");
        return R.success(permissionVO);
    }

    @ApiOperation(value = "获取所有权限", notes = "查看本项目的所有权限，页面、菜单等", response = R.class, httpMethod = "GET")
    @GetMapping
    public R getPermissionS(){
        List<PermissionVO> allPermission = permissionService.getAllPermission();
        if (allPermission != null && allPermission.size() > 0)
            return R.success(allPermission);
        return R.success(null, "当前未设置任何权限");
//        return R.success(permissionService.list());
    }


    @ApiOperation(value = "获取父权限目录", notes = "获取父权限目录", response = R.class, httpMethod = "GET")
    @GetMapping("/parent/{apply}")
    public R getParentPermissionS(@PathVariable("apply") Integer apply){
        LambdaQueryWrapper<PermissionEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PermissionEntity::getApply, apply);
        queryWrapper.eq(PermissionEntity::getParentId, 0);
        return R.success(permissionService.list(queryWrapper));
    }

//    @ApiOperation(value = "获取所有权限(checkbox使用)", response = R.class, httpMethod = "GET")
//    @GetMapping("/checkbox")
//    public R getPermissionSForCheckBox(){
//        LambdaQueryWrapper<PermissionEntity> queryWrapper = new LambdaQueryWrapper<>();
//        // 此处 checkbox 只给公司管理员为公司职工分配权限使用，因此 apply = 2
//        queryWrapper.eq(PermissionEntity::getApply, 2);
//        queryWrapper.eq(PermissionEntity::getParentId, 0);
//        List<PermissionEntity> list = permissionService.list(queryWrapper);
//        return R.success(list.stream().map((item)->{
//            PermissionCheckBoxVO permissionCheckBoxVO = new PermissionCheckBoxVO();
//            BeanUtils.copyProperties(item, permissionCheckBoxVO);
//            return permissionCheckBoxVO;
//        }).collect(Collectors.toList()));
//    }


    @ApiOperation(value = "停止使用某权限", notes = "根据Id停止使用本项目的权限，当该权限有子权限无法停止", response = R.class, httpMethod = "GET")
    @GetMapping("/stop/{permissionId}")
    public R stopPermission(
            @ApiParam(required = true, value = "权限Id")
            @PathVariable("permissionId")
            @NotEmpty(message = "必须具备权限Id") Integer permissionId){
        permissionService.delPermission(permissionId);
        return R.success();
    }

    @ApiOperation(value = "重新开启使用某权限",notes = "根据Id开启使用本项目的权限", response = R.class, httpMethod = "GET")
    @GetMapping("/start/{permissionId}")
    public R startPermission(
            @ApiParam(required = true, value = "权限Id")
            @PathVariable("permissionId")
            @NotEmpty(message = "必须具备权限Id") Integer permissionId){
        permissionService.startPermission(permissionId);
        return R.success();
    }

}

